<template>
  <a-row :gutter="24" >
    <a-col :md="5" :sm="24" >
      <a-card :bordered="false" :loading="treeLoading">
        <div v-if="this.categoryTree!='' ">
          <a-tree
            style="scroll:true"
            :treeData="categoryTree"
            v-if="categoryTree.length"
            @select="handleClick"
            :defaultExpandAll="true"
            :defaultExpandedKeys="defaultExpandedKeys"
            :replaceFields="replaceFields" />
        </div>
        <div v-else>
          <a-empty :image="simpleImage" />
        </div>
      </a-card>
    </a-col>
    <a-col :md="19" :sm="24">
      <x-card v-if="hasPerm('materialCategory:page')">
        <div slot="content" class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="8" :sm="24">
                <a-form-item label="名称">
                  <a-input v-model="queryParam.name" allow-clear placeholder="请输入名称"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="是否启用">
                  <a-select style="width: 100%" v-model="queryParam.isEnable" placeholder="请选择是否启用">
                    <a-select-option v-for="(item,index) in isEnableData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <span class="table-page-search-submitButtons">
                  <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </x-card>
      <a-card :bordered="false">
        <s-table
          ref="table"
          :columns="columns"
          :data="loadData"
          :alert="true"
          :rowKey="(record) => record.id"
          :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        >
          <template class="table-operator" slot="operator" v-if="hasPerm('materialCategory:add')" >
            <a-button type="primary" v-if="hasPerm('materialCategory:add')" icon="plus" @click="$refs.addForm.add()">新增物料分类</a-button>
          </template>
          <span slot="isEnablescopedSlots" slot-scope="text">
            {{ 'whether' | dictType(text) }}
          </span>
          <span slot="action" slot-scope="text, record">
            <a v-if="hasPerm('materialCategory:edit')" @click="$refs.editForm.edit(record)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('materialCategory:edit') & hasPerm('materialCategory:delete')"/>
            <a-popconfirm v-if="hasPerm('materialCategory:delete')" placement="topRight" title="确认删除？" @confirm="() => materialCategoryDelete(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
        </s-table>
        <add-form ref="addForm" @ok="handleOk" />
        <edit-form ref="editForm" @ok="handleOk" />
      </a-card>
    </a-col>
  </a-row>
</template>
<script>
  import { STable, XCard } from '@/components'
  import { Empty } from 'ant-design-vue'
  import { materialCategoryPage, materialCategoryDelete, getCategoryTree } from '@/api/modular/main/base/material/materialCategoryManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  export default {
    components: {
      XCard,
      STable,
      addForm,
      editForm
    },
    data () {
      return {
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '名称',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'name'
          },
          {
            title: '是否启用',
            align: 'center',
            width: 100,
            ellipsis: true,
            scopedSlots: { customRender: 'isEnablescopedSlots' },
            dataIndex: 'isEnable'
          },
          {
            title: '排序',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'sort'
          },
          {
            title: '备注',
            align: 'center',
            width: 200,
            ellipsis: true,
            dataIndex: 'remark'
          },
          {
            title: '更新人',
            align: 'center',
            width: 100,
            ellipsis: true,
            dataIndex: 'updateUserName'
          },
          {
            title: '更新时间',
            align: 'center',
            width: 150,
            ellipsis: true,
            dataIndex: 'updateTime'
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return materialCategoryPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        selectedRowKeys: [],
        selectedRows: [],
        categoryTree: [],
        isEnableData: [],
        defaultExpandedKeys: [],
        // 搜索的三个参数
        expandedKeys: [],
        searchValue: '',
        autoExpandParent: true,
        treeLoading: true,
        simpleImage: Empty.PRESENTED_IMAGE_SIMPLE,
        replaceFields: {
          key: 'id'
        }
      }
    },
    created () {
      this.getCategoryTree()
      if (this.hasPerm('materialCategory:edit') || this.hasPerm('materialCategory:delete')) {
        this.columns.push({
          title: '操作',
          align: 'center',
          width: 150,
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          fixed: 'right'
        })
      }
      const isEnableOption = this.$options
      this.isEnableData = isEnableOption.filters['dictData']('whether')
    },
    methods: {
      /**
       * 获取到分类树，展开顶级下树节点
       */
      getCategoryTree () {
        getCategoryTree(Object.assign(this.queryParam)).then(res => {
          this.treeLoading = false
          if (!res.success) {
            return
          }
          this.categoryTree = res.data
          this.queryParam.parentId = this.categoryTree[0].id
          // 全部展开，上面api方法提供的不生效，先用此方法
          for (var item of res.data) {
            // eslint-disable-next-line eqeqeq
            if (item.parentId == 0) {
              this.defaultExpandedKeys.push(item.id)
            }
          }
          this.$refs.table.refresh()
        })
      },
      materialCategoryDelete (record) {
        materialCategoryDelete(record).then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败:' + res.message)
          }
        })
      },
      handleClick (e) {
        this.queryParam = {
          pid: e.toString()
        }
        this.$refs.table.refresh(true)
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      handleOk () {
        this.getCategoryTree()
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
